﻿@model YiYiTong.Rocket.Entity.UserInfo

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<form action="@Url.Action("Register","User")" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td colspan="2">
                <h5>
                    @ViewBag.Title，请输入用户信息，标<em>*</em>号是必填或必选项。
                </h5>
                <input type="hidden" value="@Model.uid" name="UID" />
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">用户名<em>*</em></td>
            <td><input type="text" id="LoginName" name="LoginName" maxlength="20" value="@Model.loginName" /></td>
        </tr>
        <tr id="Tr_Pwd">
            <td class="tableleft">密码<em>*</em></td>
            <td><input type="password" id="Password" maxlength="32" name="Password" /></td>
        </tr>
        <tr id="Tr_AgainPwd">
            <td class="tableleft">确认密码<em>*</em></td>
            <td><input type="password" name="AgainPwd" /></td>
        </tr>
        <tr>
            <td class="tableleft">状态<em>*</em></td>
            <td>
                <input type="radio" name="UserStatus" style="vertical-align:middle; margin-bottom:6px;" value="1" checked />启用
                <input type="radio" name="UserStatus" style="vertical-align:middle; margin-bottom:6px;" value="2" />禁用
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">真实姓名</td>
            <td><input type="text" name="RealName" maxlength="10" value="@Model.realName" /></td>
        </tr>
        <tr>
            <td class="tableleft">手机号<em>*</em></td>
            <td><input type="text" name="Mobile" value="@Model.mobile" maxlength="16" /></td>
        </tr>
        <tr>
            <td class="tableleft">邮箱</td>
            <td><input type="text" name="Email" maxlength="50" value="@Model.email" /></td>
        </tr>
        <tr>
            <td class="tableleft">联系地址</td>
            <td><input type="text" name="LinkAddress" maxlength="100" value="@Model.linkAddress" /></td>
        </tr>
        <tr>
            <td class="tableleft">备注</td>
            <td><textarea name="Remark" style="width:240px;" maxlength="250" cols="4">@Model.remark</textarea></td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <p><span id="spMsg"></span></p>
                <button type="submit" class="btn btn-primary">保存</button> &nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回</button>
            </td>
        </tr>
    </table>
    <script>
        BUI.use('bui/overlay', function (overlay) {
            function showSucc(obj, url) {
                BUI.Message.Alert(obj, function () {
                    location.href = url;
                }, 'success');
            }
            $(function () {
                //绑定用户状态
                switch ('@Model.userStatus') {
                    case '1':
                        $("input[name=UserStatus]").get(0).checked = true;
                        break;
                    case '2':
                        $("input[name=UserStatus]").get(1).checked = true;
                        break;
                }

                $('#backid').on('click', function () {
                    window.document.location.href = "@Url.Action("UserList", "User")";
                });

                //判断手机号格式
                jQuery.validator.addMethod("isMobile", function (value, element) {
                    var length = value.length;
                    //var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                    var mobile = /^((1{1})+\d{10})$/;
                    return this.optional(element) || (mobile.test(value));
                }, "请正确填写您的手机号码");

                //判断中文字符
                jQuery.validator.addMethod("isChinese", function (value, element) {
                    return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);
                }, "只能包含中文字符");

                //判断只能字母或数字
                jQuery.validator.addMethod("IsNumOrLetter", function (value, element) {
                    return this.optional(element) || /^(\d|[a-zA-Z])+$/.test(value);
                }, "只能字母或数字的组合");

                //判断只能字母,数字,.符号,_下划线或-杠
                jQuery.validator.addMethod("IsPwd", function (value, element) {
                    return this.optional(element) || /^(\d|[a-zA-Z\_])+$/.test(value);
                }, "只能字母,数字,.符号,_下划线或-杠的组合");

                var uid = '@Model.uid';
                if (uid > 0) {
                    //用户名文本框设置只读
                    $("#LoginName").attr("readonly", "readonly");
                    //密码文本框隐藏
                    $("#Tr_Pwd").hide();
                    $("#Tr_AgainPwd").hide();
                    $('#Tr_TwoPwd').hide();

                    var validator = $('form').validate({
                        errorLabelContainer: $("#errorContainer"),
                        errorElement: "span",
                        rules: {
                            RealName: { isChinese: true },
                            Mobile: { required: true, isMobile: true },
                            Email: { email: true }
                        },
                        messages: {
                            RealName: { isChinese: "真实姓名只能包含中文字符" },
                            Mobile: { required: "手机号码不能为空", isMobile: "手机号格式错误" },
                            Email: { email: "邮箱格式错误" }
                        },
                        submitHandler: function (form) {
                            $(form).ajaxSubmit(function (result) {
                                if (result.IsSuccess) {
                                    showSucc(result.Message, result.Body);
                                }
                                else {
                                    $("#spMsg").html(result.Message);
                                }
                                return false;
                            });
                        }
                    });
                }
                else {
                    var validator = $('form').validate({
                        errorLabelContainer: $("#errorContainer"),
                        errorElement: "span",
                        rules: {
                            LoginName: { required: true, rangelength: [5, 20], IsNumOrLetter: true },
                            RealName: { isChinese: true },
                            Password: { required: true, rangelength: [6, 20], IsPwd: true },
                            AgainPwd: { required: true, equalTo: '#Password', IsPwd: true },
                            Mobile: { required: true, isMobile: true },
                            Email: { email: true },
                            TwoPwd: { required: true, rangelength: [6, 20], IsPwd: true }
                        },
                        messages: {
                            LoginName: { required: "用户名不能为空", rangelength: "5-20个字符", IsNumOrLetter: "只能字母或数字的组合" },
                            RealName: { isChinese: "真实姓名只能包含中文字符" },
                            Password: { required: "密码不能为空", rangelength: "密码6-20个字符", IsPwd: "只能是字母,数字，下划线的组合" },
                            AgainPwd: { required: "确认密码不能为空", equalTo: '密码不一致', IsPwd: "只能是字母,数字，下划线的组合" },
                            Mobile: { required: "手机号码不能为空", isMobile: "手机号格式错误" },
                            Email: { email: "邮箱格式错误" },
                            TwoPwd: { required: "二次密码不能为空", rangelength: "二次密码6-20个字符", IsPwd: "只能是字母,数字，下划线的组合" }
                        },
                        submitHandler: function (form) {
                            $(form).ajaxSubmit(function (result) {
                                if (result.IsSuccess) {
                                    showSucc(result.Message, result.Body);
                                }
                                else {
                                    $("#spMsg").html(result.Message);
                                }
                                return false;
                            });
                        }
                    });
                }
            });
        });
    </script>
</form>
